import { Room } from '../api/Room';
import {
	add_operation,
	AdditionalOperation,
	apply,
	RulesLogic,
} from 'json-logic-js';
import { CardRegistry } from '../registry/CardRegistry';
import { events } from '../utils/events';
import { logger } from '../utils/logger';
import { CardEvent } from '../api/CardEvent';

export class CardCommandExecutor {
	constructor(
		private room: Room,
		private user: string,
		private target: string,
	) {
		this.register();
	}

	/**
	 * 注册API
	 */
	private register() {
		// 结束回合
		add_operation('endTurn', (deptId) => {
			const dept = this.getDept(deptId);
			if (dept) dept.round = true;
		});

		// 随机
		add_operation('random', (...args) => {
			if (args.length === 0) {
				return Math.random();
			} else if (args.length === 1) {
				const max = args[0];
				return Math.floor(Math.random() * (max + 1));
			} else if (args.length === 2) {
				const min = args[0];
				const max = args[1];
				return Math.floor(Math.random() * (max - min + 1)) + min; // 包括min和max
			}
		});

		// 添加数据
		add_operation('addData', (deptId, key, value) => {
			const dept = this.getDept(deptId);
			if (dept) dept.addDeptDataValue(key, Number(value));
		});

		// 获取数据
		add_operation('getData', (deptId, key) => {
			const dept = this.getDept(deptId);
			if (!dept) return 0;
			return dept.getDeptDataValue(key);
		});

		// 发送游戏消息
		add_operation('sendMessage', (message) => {
			this.room.send('message', this.resolve(message));
		});

		// 添加势力
		add_operation('addPower', (deptId, amount) => {
			const dept = this.getDept(deptId);
			if (dept) dept.power += Number(amount);
		});

		// 添加卡牌
		add_operation('addCard', (deptId, cardId, count = 1) => {
			const dept = this.getDept(deptId);
			if (!dept) return;
			const card = CardRegistry.get(cardId);
			if (!card) return;
			for (let i = 0; i < count; i++) {
				CardEvent.addCard(this.room, dept.deptId, card);
			}
		});

		// 获取部门势力
		add_operation('getDeptPower', (deptId) => {
			const dept = this.getDept(deptId);
			if (!dept) return 0;
			return dept.power;
		});

		// 获取部门行动点
		add_operation('getDeptPoint', (deptId) => {
			const dept = this.getDept(deptId);
			if (!dept) return 0;
			return dept.point;
		});

		// 获取部门最大行动点
		add_operation('getDeptPointMax', (deptId) => {
			const dept = this.getDept(deptId);
			if (!dept) return 0;
			return dept.pointMax;
		});

		// 获取部门名称
		add_operation('getDeptName', (deptId) => {
			const dept = this.getDept(deptId);
			if (!dept) return '未知部门';
			return dept.name;
		});

		// 抽牌
		add_operation('drawCard', (deptId, pool, count, global) => {
			const dept = this.getDept(deptId);
			if (!dept) return;
			CardEvent.drawCard(this.room, deptId, pool, count, global);
		});
	}

	/**
	 * 获取部门
	 * @param deptId
	 * @returns
	 */
	private getDept(deptId: string) {
		return this.room.getDept(this.resolve(deptId));
	}

	/**
	 * 解析占位符
	 * @param input
	 * @returns
	 */
	private resolve(input: string) {
		return input
			.replace(/%user%/g, this.user)
			.replace(/%target%/g, this.target);
	}

	/**
	 * 执行卡牌逻辑
	 */
	execute(logic: RulesLogic<AdditionalOperation>) {
		const api = {};
		return apply(logic, api);
	}
}
